part of 'widgets.dart';

/// Svg会先界定一个正方形的框，也就是size，然后根据scale决定最终比例
/// Svg的长边碰到size*scale时，就是它的最终尺寸
class LxSvgAsset extends StatelessWidget {
  final String path;
  final Color color;

  /// 如果parent没有约束，则必须传入size，svg的尺寸为先碰到size为止
  final double? size;
  final double ratio;

  /// svg图片
  ///
  /// 默认会填充parent widget的限制
  ///
  /// 如果有scale，则会在限制的基础上等比例缩放
  const LxSvgAsset(
    this.path, {
    required this.color,
    this.size,
    this.ratio = 1,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    assert(ratio > 0 && ratio <= 1, "scale为图片放大倍数，必须在(0,1]范围");

    return LayoutBuilder(
      builder: (context, constraints) {
        double imgSize =
            size ?? min(constraints.maxWidth, constraints.maxHeight);

        return SvgPicture.asset(
          path,
          width: imgSize * ratio,
          height: imgSize * ratio,
          color: color,
        );
      },
    );
  }
}
